/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.method;


import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.repository.adaptor.INestedAdaptor;
import com.inspur.edp.cef.generator.repository.dac.DataTypeDacGenerator;
import com.inspur.edp.cef.generator.repository.dac.NestedDacGenContext;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class GetNestedAdaptorGenerator extends GetAdaptorGenerator {
	private NestedDacGenContext parentContext;

	public GetNestedAdaptorGenerator(String fieldName, NestedDacGenContext parentContext) {
		super(fieldName, parentContext);
		this.parentContext = parentContext;
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(INestedAdaptor.class.getTypeName());
	}

	@Override
	protected String getMethodName() {
		return "getAdaptor";
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected Block buildMethodBody() {
		String typeName = ((NestedDacGenContext) dataTypeDacGenContext).getGeneratedAdaptorType().getFullName();
		ClassInstanceCreation createInstance = ast.newClassInstanceCreation();
		createInstance.setType(ast.newSimpleType(ast.newName(typeName)));
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(createInstance);
		Block block = ast.newBlock();
		block.statements().add(returnStatement);
		return block;
	}

	@Override
	protected IfStatement buildInitStatement() {
		IfStatement ifStatement=ast.newIfStatement();
		setIfStatementExpression(ifStatement);
		setAssignAdapter(ifStatement);
		return ifStatement;
	}

	private void setIfStatementExpression(IfStatement ifStatement) {
		InfixExpression infixExpression=ast.newInfixExpression();
		infixExpression.setLeftOperand(ast.newSimpleName(DataTypeDacGenerator.AdaptorFieldName));
		infixExpression.setOperator(InfixExpression.Operator.EQUALS);
		infixExpression.setRightOperand(ast.newNullLiteral());

		ifStatement.setExpression(infixExpression);
	}

	private void setAssignAdapter(IfStatement ifStatement) {
		Block block=ast.newBlock();
		Assignment assignment=ast.newAssignment();
		assignment.setLeftHandSide(ast.newSimpleName(DataTypeDacGenerator.AdaptorFieldName));
		assignment.setOperator(Assignment.Operator.ASSIGN);

		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newName(parentContext.getGeneratedAdaptorType().getFullName())));
		assignment.setRightHandSide(classInstanceCreation);
		block.statements().add(ast.newExpressionStatement(assignment));

		ifStatement.setThenStatement(block);
	}
}
